<template>
	<view class="order" :style="'background-color: #f7f7f7;color:#333333;'+'min-height:'+$xyfun.xysys().windowHeight+'px'">
		<view class="tab flex tc tb p-tb-25" :style="'background-color: #ffffff;'">
			<view v-for="(item,index) in orderStatus" :class="'col-'+orderStatus.length" @tap="setTab(index)">
				<view :style="orderStatusIndex == index ? 'color:#00dd83;' : 'color:#333333;'">
					{{item.name}}
					<view class="line" :style="orderStatusIndex == index ? 'background-color: #00dd83;' : 'background-color: #ffffff;'"></view>
				</view>
			</view>
		</view>
		<block v-if="!isLoading">
			<view class="order-list p-tb-30" v-if="!isEmpty">
				<view class="item br-10 m-lr-30 m-b-30 p-30 p-t" :style="'background-color: #ffffff;'" v-for="item in orderList" :key="item.id" @tap="$xyfun.to('/pages/user/order/detail?id='+item.id+'&type=goods')">
					<view class="flex">
						<view>订单号：{{item.order_sn}}</view>
						<view class="m-l-auto tb" :style="'color:#00dd83;'">{{item.status_text}}</view>
					</view>
					
					<view class="goods flex m-t-30">
						<view v-for="(goods,index) in item.item" class="ovh br-10 m-r-20 image" v-if="index<3">
							<image :src="goods.goods_image" mode="widthFix" />
						</view>
						<text class="xyicon icon-right m-l-auto"></text>
					</view>
					
					<view :style="'background-color: #f7f7f7;'" class="flex p-20 m-t-30">
						<view>共{{item.total_num}}件商品</view>
						<view class="m-l-auto">实付款：¥{{item.total_fee}}</view>
					</view>
				</view>
			</view>
			<view v-else>
				<xy-empty :text="'暂无'+(orderStatusIndex>0?orderStatus[orderStatusIndex].name:'')+'订单'" />
			</view>
		</block>
	</view>
</template>

<script>
	import { mapState } from 'vuex';
	import xyEmpty from '@/components/xy-empty';
	export default {
		components: {
			xyEmpty,
		},
		data() {
			return {

				isLoading:true,
				isEmpty: true,
				orderList: [],
				currentPage: 1,
				lastPage: 1,
				loadStatus: 'loadmore',
				orderStatus:[
					{value:'all',name:'全部'},
					{value:'0',name:'待付款'},
					{value:'1',name:'待发货'},
					{value:'2',name:'待收货'},
					{value:'5',name:'已完成'},
					{value:'-1',name:'已取消'},
				],
				orderStatusIndex:0,
			}
		},
		computed: {
			...mapState(['common'])
		},
		async onLoad(options) {
			
			console.log(options);
			
			if(options.status != undefined){
				this.orderStatusIndex = options.status;
			}
			

			this.loadData();
		},
		onPullDownRefresh() {
			this.currentPage = 1;
			this.orderList = [];
			this.loadData();
		},
		onReachBottom() {
			if(this.currentPage < this.lastPage) {
				this.currentPage += 1;
				this.loadData();
			}
		},
		methods: {
			async loadData(){
				this.$api.post({
					url: '/goods_order/lists',
					loadingTip:'加载中...',
					data: {
						page: this.currentPage,
						status: this.orderStatus[this.orderStatusIndex]['value']
					},
					success: res => {
						uni.stopPullDownRefresh();
						this.isLoading = false;
						this.orderList = [...this.orderList, ...res.data];
						this.isEmpty = !this.orderList.length;
						this.currentPage = res.current_page; 
						this.lastPage = res.last_page;
						this.loadStatus = this.currentPage < res.last_page ? 'loadmore' : 'nomore';
					}
				});
			},
			
			
			setTab(index){
				this.orderStatusIndex = index;
				this.currentPage = 1;
				this.orderList = [];
				this.loadData();
			},
			
		}
	}
</script>

<style scoped lang="scss">
	.tab{
		width: 100%;
		.line{height: 4rpx;width: 60rpx;margin: 10rpx auto 0;}
	}
	
	.order-list{
		.goods{
			line-height: 150rpx;
			.image{width: 150rpx;height: 150rpx;}
			.image image{width: 150rpx;}
		}
	}
	
</style>